#include "chessboard.h"

chessboard::chessboard(QWidget *parent) : QWidget(parent)
{
    /* 清空棋盘 */
    memset(m_point, 0, sizeof(m_point));

    for (int idx = 0; idx < m_col; idx ++)  //置为空
    {
        for (int jdx = 0; jdx < m_row; jdx++)
        {
            setPointVal(idx, jdx, EMPTY);
        }
    }

    m_highlightColor = QColor(175, 238, 238);
}

void chessboard::paintEvent(QPaintEvent * event)
{
    QPainter painter(this);

    /* 绘制棋盘颜色 */
    QColor backgroundColor(222, 184, 135);//255, 254, 224
    painter.fillRect(rect(), backgroundColor);

    /* 横线 */
    for (int idx = 0; idx < m_row; idx++)
    {
        painter.drawLine(m_margin, m_margin + idx * m_cellSize, m_margin + (m_col - 1) * m_cellSize, m_margin + idx * m_cellSize);
    }

    /* 竖线 */
    for (int idx = 0; idx < m_col; idx++)
    {
        if (idx == 0 || idx == m_col - 1)
        {
            painter.drawLine(m_margin + idx * m_cellSize, m_margin, m_margin + idx * m_cellSize, m_margin + (m_row - 1) * m_cellSize);
        }
        else
        {
            painter.drawLine(m_margin + idx * m_cellSize, m_margin, m_margin + idx * m_cellSize, m_margin + (m_row / 2 - 1) * m_cellSize);
            painter.drawLine(m_margin + idx * m_cellSize, m_margin + (m_row / 2) * m_cellSize, m_margin + idx * m_cellSize, m_margin + (m_row - 1) * m_cellSize);
        }
    }

    /* 九宫格斜线 */
    painter.drawLine(m_margin + 3 * m_cellSize, m_margin, m_margin + 5 * m_cellSize, m_margin + 2 * m_cellSize);
    painter.drawLine(m_margin + 3 * m_cellSize, m_margin + 2 * m_cellSize, m_margin + 5 * m_cellSize, m_margin);
    painter.drawLine(m_margin + 3 * m_cellSize, m_margin + 7 * m_cellSize, m_margin + 5 * m_cellSize, m_margin + (m_row - 1) * m_cellSize);
    painter.drawLine(m_margin + 3 * m_cellSize, m_margin + (m_row - 1) * m_cellSize, m_margin + 5 * m_cellSize, m_margin + 7 * m_cellSize);

    /* 设置画笔 */
    QPen pen;
    pen.setWidth(2);
    painter.setPen(pen);

    /* 定点 */
    for (int idx = 0; idx < m_col; idx++)
    {
        if (idx % 2 == 0 && idx != m_col - 1)
        {
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 3 * m_cellSize - 30, m_margin + idx * m_cellSize + 10, m_margin + 3 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 3 * m_cellSize - 10, m_margin + idx * m_cellSize + 30, m_margin + 3 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 3 * m_cellSize + 30, m_margin + idx * m_cellSize + 10, m_margin + 3 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 3 * m_cellSize + 10, m_margin + idx * m_cellSize + 30, m_margin + 3 * m_cellSize + 10);

            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 6 * m_cellSize - 30, m_margin + idx * m_cellSize + 10, m_margin + 6 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 6 * m_cellSize - 10, m_margin + idx * m_cellSize + 30, m_margin + 6 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 6 * m_cellSize + 30, m_margin + idx * m_cellSize + 10, m_margin + 6 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 6 * m_cellSize + 10, m_margin + idx * m_cellSize + 30, m_margin + 6 * m_cellSize + 10);
        }
        if (idx % 2 == 0 && idx != 0)
        {
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 3 * m_cellSize - 30, m_margin + idx * m_cellSize - 10, m_margin + 3 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 3 * m_cellSize - 10, m_margin + idx * m_cellSize - 30, m_margin + 3 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 3 * m_cellSize + 30, m_margin + idx * m_cellSize - 10, m_margin + 3 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 3 * m_cellSize + 10, m_margin + idx * m_cellSize - 30, m_margin + 3 * m_cellSize + 10);

            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 6 * m_cellSize - 30, m_margin + idx * m_cellSize - 10, m_margin + 6 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 6 * m_cellSize - 10, m_margin + idx * m_cellSize - 30, m_margin + 6 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 6 * m_cellSize + 30, m_margin + idx * m_cellSize - 10, m_margin + 6 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 6 * m_cellSize + 10, m_margin + idx * m_cellSize - 30, m_margin + 6 * m_cellSize + 10);
        }
        if (idx == 1 || idx == m_col - 2)
        {
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 2 * m_cellSize - 30, m_margin + idx * m_cellSize + 10, m_margin + 2 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 2 * m_cellSize - 10, m_margin + idx * m_cellSize + 30, m_margin + 2 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 2 * m_cellSize + 30, m_margin + idx * m_cellSize + 10, m_margin + 2 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 2 * m_cellSize + 10, m_margin + idx * m_cellSize + 30, m_margin + 2 * m_cellSize + 10);

            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 7 * m_cellSize - 30, m_margin + idx * m_cellSize + 10, m_margin + 7 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 7 * m_cellSize - 10, m_margin + idx * m_cellSize + 30, m_margin + 7 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 7 * m_cellSize + 30, m_margin + idx * m_cellSize + 10, m_margin + 7 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize + 10, m_margin + 7 * m_cellSize + 10, m_margin + idx * m_cellSize + 30, m_margin + 7 * m_cellSize + 10);

            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 2 * m_cellSize - 30, m_margin + idx * m_cellSize - 10, m_margin + 2 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 2 * m_cellSize - 10, m_margin + idx * m_cellSize - 30, m_margin + 2 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 2 * m_cellSize + 30, m_margin + idx * m_cellSize - 10, m_margin + 2 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 2 * m_cellSize + 10, m_margin + idx * m_cellSize - 30, m_margin + 2 * m_cellSize + 10);

            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 7 * m_cellSize - 30, m_margin + idx * m_cellSize - 10, m_margin + 7 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 7 * m_cellSize - 10, m_margin + idx * m_cellSize - 30, m_margin + 7 * m_cellSize - 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 7 * m_cellSize + 30, m_margin + idx * m_cellSize - 10, m_margin + 7 * m_cellSize + 10);
            painter.drawLine(m_margin + idx * m_cellSize - 10, m_margin + 7 * m_cellSize + 10, m_margin + idx * m_cellSize - 30, m_margin + 7 * m_cellSize + 10);
        }
    }

    /* 外框 */
    painter.drawLine(m_margin - 10, m_margin - 10, m_margin + (m_col - 1) * m_cellSize + 10, m_margin - 10);
    painter.drawLine(m_margin + (m_col - 1) * m_cellSize + 10, m_margin - 10, m_margin + (m_col - 1) * m_cellSize + 10, m_margin + (m_row - 1) * m_cellSize + 10);
    painter.drawLine(m_margin + (m_col - 1) * m_cellSize + 10, m_margin + (m_row - 1) * m_cellSize + 10, m_margin - 10, m_margin + (m_row - 1) * m_cellSize + 10);
    painter.drawLine(m_margin - 10, m_margin + (m_row - 1) * m_cellSize + 10, m_margin - 10, m_margin - 10);

    /* 楚河 & 汉界 */
    QFont font("华文行楷", 50, QFont::Bold);
    painter.setFont(font);
    painter.drawText(190, 522, "楚  河");

    painter.save();// 保存当前的变换状态

    painter.translate(width() / 2, height());
    painter.rotate(180);// 反转汉界 - 翻转180度

    painter.drawText(-190, 572, "汉  界");// 绘制汉界 - 反向

    painter.restore();// 恢复之前的变换状态

    /* 绘制棋子 */
    QRect rect;
    for (int idx = 0; idx < m_col; idx++)
    {
        for (int jdx = 0; jdx < m_row; jdx++)
        {

            switch (m_point[idx][jdx])
            {
            case RedCart:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "俥", QTextOption(Qt::AlignCenter));
            }
                break;
            case RedHorse:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "傌", QTextOption(Qt::AlignCenter));
            }
                break;
            case RedPhase:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "相", QTextOption(Qt::AlignCenter));
            }
                break;
            case RedChevalier:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "仕", QTextOption(Qt::AlignCenter));
            }
                break;
            case RedGenerals:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "帥", QTextOption(Qt::AlignCenter));
            }
                break;
            case RedSoldier:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "兵", QTextOption(Qt::AlignCenter));
            }
                break;
            case RedCannon:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::red);
                painter.drawText(rect, "炮", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackCart:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "車", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackHorse:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "馬", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackPhase:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "象", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackChevalier:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "士", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackGenerals:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "将", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackSoldier:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "卒", QTextOption(Qt::AlignCenter));
            }
                break;
            case BlackCannon:
            {
                painter.setPen(Qt::black);
                rect = QRect(m_margin + idx * m_cellSize - m_cellSize / 2 + 20, m_margin + jdx * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
                painter.setBrush(QColor(255, 203, 115));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 5, m_margin + jdx * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
                painter.setBrush(QColor(255, 224, 173));
                painter.drawEllipse(m_margin + idx * m_cellSize - m_cellSize / 2 + 15, m_margin + jdx * m_cellSize - m_cellSize / 2 + 15, m_cellSize - 30, m_cellSize - 30);
                painter.setPen(Qt::black);
                painter.drawText(rect, "砲", QTextOption(Qt::AlignCenter));
            }
                break;
            }
        }
    }

    /* 选中高亮 */
    if (m_isChose == true)
    {
        rect = QRect(m_margin + m_oldRow * m_cellSize - m_cellSize / 2 + 20, m_margin + m_oldCol * m_cellSize - m_cellSize / 2 + 20, m_cellSize - 40, m_cellSize - 40);
        painter.setBrush(m_highlightColor);
        painter.drawEllipse(m_margin + m_oldRow * m_cellSize - m_cellSize / 2 + 5, m_margin + m_oldCol * m_cellSize - m_cellSize / 2 + 5, m_cellSize - 10, m_cellSize - 10);
        QString str = conversion(getPointVal(m_oldRow, m_oldCol));
        painter.drawText(rect, str, QTextOption(Qt::AlignCenter));
    }
}

void chessboard::mousePressEvent(QMouseEvent * event)
{
    int row = (event->x() - m_margin + m_cellSize / 2) / m_cellSize;
    int col = (event->y() - m_margin + m_cellSize / 2) / m_cellSize;

    if (m_isRed == true)    //红方回合
    {
        if ((row >= 0 && row < m_col) && (col >= 0 && col < m_row) && getPointVal(row, col) >= RedSoldier && getPointVal(row, col) <= RedGenerals && m_isChose == false && m_cnt == 1)  //如果点击位置在棋盘内，是红棋子，未选中棋子，第一次点击
        {
            m_isChose = !m_isChose;     //选中棋子
            m_oldRow = row;
            m_oldCol = col;
            m_cnt = 2;  //第二次点击
            update();   //更新画面
        }
        else if ((row >= 0 && row < m_col) && (row != m_oldRow || col != m_oldCol) && (col >= 0 && col < m_row) && m_isChose == true && m_cnt == 2) //如果点击位置在棋盘内，不是相同的位置，选中棋子，第二次点击
        {
            emit cellClicked(m_oldRow, m_oldCol, row, col);
            if (m_isValid == true)
            {
                if (getPointVal(row, col) == BlackGenerals)
                {
                    m_isgameOver = true;
                }
                QPoint oldPoint(m_oldRow, m_oldCol);
                QPoint newPoint(row, col);
                int oldNum = getPointVal(m_oldRow, m_oldCol);
                int newNum = getPointVal(row, col);

                setPointVal(row, col, getPointVal(m_oldRow, m_oldCol));
                setPointVal(m_oldRow, m_oldCol, EMPTY);
                m_isChose = !m_isChose;
                insertSteps(oldPoint, newPoint, oldNum, newNum);
                update();
                m_isRed = false;

                if (m_isgameOver == true)
                {
                    QMessageBox::information(this, "游戏结束", "红方获胜");
                    m_cnt = 0;
                }
                else    //红方回合结束
                {
                    m_cnt = 1;
                    m_isValid = false;
                    emit whereShow();
                }
            }
        }
        else if ((row >= 0 && row < m_col) && (col >= 0 && col < m_row) && (row == m_oldRow || col == m_oldCol) && m_isChose == true && m_cnt == 2) //点击相同位置取消选中状态
        {
            m_isChose = !m_isChose;
            m_cnt = 1;
            update();
        }
    }
    else if (m_isRed == false)    //黑方回合
    {
        if ((row >= 0 && row < m_col) && (col >= 0 && col < m_row) && getPointVal(row, col) >= BlackSoldier && getPointVal(row, col) <= BlackGenerals && m_isChose == false && m_cnt == 1)
        {
            m_isChose = !m_isChose;     //选中棋子
            m_oldRow = row;
            m_oldCol = col;
            m_cnt = 2;
            update();
        }
        else if ((row >= 0 && row < m_col) && (col >= 0 && col < m_row) && (row != m_oldRow || col != m_oldCol) && m_isChose == true && m_cnt == 2)
        {
            emit cellClicked(m_oldRow, m_oldCol, row, col);
            if (m_isValid == true)
            {
                if (getPointVal(row, col) == RedGenerals)
                {
                    m_isgameOver = true;
                }
                QPoint oldPoint(m_oldRow, m_oldCol);
                QPoint newPoint(row, col);
                int oldNum = getPointVal(m_oldRow, m_oldCol);
                int newNum = getPointVal(row, col);

                setPointVal(row, col, getPointVal(m_oldRow, m_oldCol));
                setPointVal(m_oldRow, m_oldCol, EMPTY);
                m_isChose = !m_isChose;
                insertSteps(oldPoint, newPoint, oldNum, newNum);
                update();
                m_isRed = true;

                if (m_isgameOver == true)
                {
                    QMessageBox::information(this, "游戏结束", "黑方获胜");
                    m_cnt = 0;
                }
                else    //黑方回合结束
                {
                    m_cnt = 1;
                    m_isValid = false;
                    emit whereShow();
                }
            }
        }
        else if ((row >= 0 && row < m_col) && (col >= 0 && col < m_row) && (row == m_oldRow || col == m_oldCol) && m_isChose == true && m_cnt == 2)
        {
            m_isChose = !m_isChose;
            m_cnt = 1;
            update();
        }
    }
}

Chess chessboard::getPointVal(int row, int col)
{
    return (Chess)m_point[row][col];
}

void chessboard::setPointVal(int row, int col, Chess chess)
{
    m_point[row][col] = chess;
}

void chessboard::setValid()
{
    m_isValid = !m_isValid;
}

bool chessboard::getValid()
{
    return m_isValid;
}

bool chessboard::isRed(int row, int col)
{
    if (m_point[row][col] >= RedSoldier && m_point[row][col] <= RedGenerals)
    {
        return true;
    }
    return false;
}

bool chessboard::isBlack(int row, int col)
{
    if (m_point[row][col] >= BlackSoldier && m_point[row][col] <= BlackGenerals)
    {
        return true;
    }
    return false;
}

void chessboard::initialize()
{
    m_isRed = true;
    m_isChose = false;
    m_cnt = 1;
    m_isValid = false;
    m_isgameOver = false;
    steps.clear();  //每次开始都要将上一次存入的数据清空
}

int chessboard::getRow()
{
    return m_row;
}

int chessboard::getCol()
{
    return m_col;
}

QString chessboard::conversion(Chess chess)
{
    QString str;
    switch (chess)
    {
    case 1:
        str = "兵";
        break;
    case 2:
        str = "炮";
        break;
    case 3:
        str = "俥";
        break;
    case 4:
        str = "傌";
        break;
    case 5:
        str = "相";
        break;
    case 6:
        str = "仕";
        break;
    case 7:
        str = "帥";
        break;
    case 8:
        str = "卒";
        break;
    case 9:
        str = "砲";
        break;
    case 10:
        str = "車";
        break;
    case 11:
        str = "馬";
        break;
    case 12:
        str = "象";
        break;
    case 13:
        str = "士";
        break;
    case 14:
        str = "将";
        break;
    }
    return str;
}

/* 入栈 */
void chessboard::insertSteps(QPoint &oldPoint, QPoint &newPoint, int &oldChess, int &newChess)
{
    Steps step;
    step.setOldPoint(oldPoint);
    step.setNewPoint(newPoint);
    step.setOldChess(oldChess);
    step.setNewChess(newChess);

    steps.push(step);
}

/* 出栈 */
void chessboard::removeSteps()
{
    if (steps.empty() == false && m_isgameOver == false)    //游戏结束不能悔棋
    {
        Steps step = steps.top();
        QPoint oldPoint = step.getOldPoint();
        QPoint newPoint = step.getNewPoint();
        int oldChess = step.getOldChess();
        int newChess = step.getNewChess();

        setPointVal(oldPoint.x(), oldPoint.y(), (Chess)oldChess);
        setPointVal(newPoint.x(), newPoint.y(), (Chess)newChess);

        steps.pop();
        m_isRed = !m_isRed;
        m_isChose = false;
        m_cnt = 1;
        emit whereShow();
        update();
    }
}

bool chessboard::isRed()
{
    return m_isRed;
}
